#!/usr/bin/env python
#
# Arkaya Arcade Frontend
#
# http://www.strickerz.dk
#
# Released under the GNU General Public License
try:
	import pygame
	import os
	import arkaya_resources
	import math
	import ConfigParser
	from constants import *
except ImportError, err:
	print "couldn't load module. %s" % (err)
	sys.exit(2)


class GameEngine(pygame.sprite.Sprite):
	"""A game engine represented by an image
	Returns: GameEngine object
	Functions: update"""

	def __init__(self, name, emulator):
		pygame.sprite.Sprite.__init__(self)
		
		emulator_ini = os.path.join(APP_PATH, "config", emulator + ".ini")

		parser = ConfigParser.ConfigParser()		
		parser.read(emulator_ini)
		
		self.image, self.rect = arkaya_resources.load_png(parser.get("artwork", "logo"))
		self.name = name
		self.executable = parser.get("emulator", "emulator_executable")
		self.emulator_params = parser.get("emulator", "emulator_params")
		self.rom_path = parser.get("emulator", "rom_path")
		self.rom_file = parser.get("emulator", "rom_file")
		self.titles_path = parser.get("artwork", "artwork_1_path")
		
		self.rect.left = 640 # SCREEN_WIDTH
		self.rect.top = math.floor((480 - self.rect.height) / 2) # SCREEN_HEIGHT - self.rect.height / 2
		self.area = self.rect
		self.status = "_DEFROSTED"
		self.i = 0
		self.direction = "L"

	def zero(self, direction):
		self.direction = direction
		self.status = "_DEFROSTED"
		if direction == "L":
			self.rect.topleft = 640, math.floor((480 - self.rect.height) / 2)
		else:
			self.rect.topleft = -self.rect.width, math.floor((480 - self.rect.height) / 2)

	def update(self):
		if not self.isFrozen():
			if self.direction == "L":
				self.i = -40
			elif self.direction == "R":
				self.i = 40

			if self.direction == "L":
				if self.rect.left <= (640 - self.rect.width) / 2 and self.status != "_THAWING":
					self.i = 0
					self.status = "_FROZEN"
			elif self.direction == "R":
				if self.rect.left >= (640 - self.rect.width) / 2 and self.status != "_THAWING":
					self.i = 0
					self.status = "_FROZEN"
		
			newpos = self.rect.move((self.i, 0))
			self.rect = newpos
	
	def setImage(self, image):
		self.image, self.rect = arkaya_resources.load_png(image)
	
	def getName(self):
		return self.name
	
	def setStatus(self, status):
		self.status = status

	def getStatus(self):
		return self.status

	def isFrozen(self):
		if self.status == "_FROZEN":
			return True
		else:
			return False

	def thaw(self, direction):
		if self.isFrozen():
			self.direction = direction
			self.status = "_THAWING"


class TitleSnap(pygame.sprite.Sprite):

	def __init__(self, image, gameengine):
		self.gameengine = gameengine
		self.title_snap_left = TITLE_SNAP_LEFT
		self.title_snap_top = TITLE_SNAP_TOP
		self.title_snap_width = TITLE_SNAP_WIDTH
		self.title_snap_height = TITLE_SNAP_HEIGHT
		pygame.sprite.Sprite.__init__(self)
		self.image, self.rect = arkaya_resources.load_constrained_png(image, self.gameengine.titles_path, TITLE_SNAP_WIDTH - 15, TITLE_SNAP_HEIGHT - 15)
		self.rect.left = self.calcLeft(self.rect)
		self.rect.top = self.calcTop(self.rect)
		self.rect.width = 40
		self.rect.height = 30
		self.area = self.rect
		
	def setImage(self, image, gameengine):
		self.gameengine = gameengine

		self.image, self.rect = arkaya_resources.load_constrained_png(image, self.gameengine.titles_path, TITLE_SNAP_WIDTH - 30, TITLE_SNAP_HEIGHT - 30)

		self.rect.left = self.calcLeft(self.rect)
		self.rect.top = self.calcTop(self.rect)
	
	def calcTop(self, rect):
		margin = ((self.title_snap_height - rect.height) / 2) + self.title_snap_top

		return margin
	
	def calcLeft(self, rect):
		margin = ((self.title_snap_width - rect.width) / 2) + self.title_snap_left

		return margin


class RotatingLogo(pygame.sprite.Sprite):
	""" Endscreen rotating logo """
	def __init__(self):
		pygame.sprite.Sprite.__init__(self)
		self.image, self.rect = arkaya_resources.load_png('roto.png')
		self.rect.center = SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2
		self.angle = 0
		self.scale = 0
		self.rotate = True
		self.clear = False
		self.original = self.image

	def update(self):
		if self.rotate:
			self._rotate()
		elif self.clear is False:
			self._remove()

	def reset(self):
		self.angle = 0
		self.scale = 0
		self.rotate = True

	def _rotate(self):
		self.angle += 12
		self.scale += 0.016

		if self.angle > 720:
			self.angle = 0
			self.scale = 1
			self.rotate = False

		self.image = pygame.transform.rotozoom(self.original, self.angle, self.scale)
		self.rect = self.image.get_rect(center = self.rect.center)

	def _remove(self):
		if self.rect.top < 0 - self.rect.height:
			self.clear = True
		else:
			newpos = self.rect.move(0, -5)
			self.rect = newpos


class MovingArrow(pygame.sprite.Sprite):
	""" An arrow moving	"""
	def __init__(self, direction):
		self.direction = direction
		pygame.sprite.Sprite.__init__(self)
		if self.direction == 'LEFT':
			self.image, self.rect = arkaya_resources.load_png('left.png')
			self.rect.topleft = 10, (SCREEN_HEIGHT / 2) - math.floor(self.rect.height / 2) # (SCREEN_HEIGHT / 2) - (HEIGHT_OF_SPRITE / 2)
			self.area = pygame.Rect((self.rect.topleft), (50, 75))
		if self.direction == 'RIGHT':
			self.image, self.rect = arkaya_resources.load_png('right.png')
			self.rect.topright = pygame.display.get_surface().get_width() - 10, (SCREEN_HEIGHT / 2) - math.floor(self.rect.height / 2)
			self.area = pygame.Rect((self.rect.topleft), (5, 75))
		self.i = 1

	def update(self):
		self._glide()

	def _glide(self):
		if self.direction == 'LEFT':
			if self.rect.right == self.area.right:
				self.i = -1
			elif self.rect.left == self.area.left:
				self.i = 1

		if self.direction == 'RIGHT':
			if self.rect.right == self.area.left + 30:
				self.i = 1
			elif self.rect.left == self.area.left:
				self.i = -1
		# When self.i is positive sprite moves right
		newpos = self.rect.move((self.i, 0))
		self.rect = newpos
